<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="fontsize: 14px">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-button type="primary" @click="showAddDialog = true"
          >添加角色</el-button
        >
      </el-row>
      <el-table :data="roleList" style="width: 100%" :border="true">
        <el-table-column type="expand">
          <template slot-scope="{ row }">
            <el-row
              type="flex"
              align="middle"
              :class="['bgbottom', index === 0 ? 'bgtop' : '']"
              v-for="(item1, index) in row.children"
              :key="item1.id"
            >
              <el-col :span="8">
                <el-tag>{{ item1.authName }}</el-tag
                ><i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="16">
                <el-row
                  type="flex"
                  align="middle"
                  v-for="(item2, i) in item1.children"
                  :key="item2.id"
                  :class="[i === 0 ? '' : 'bgtop']"
                >
                  <el-col :span="6">
                    <el-tag type="success">{{ item2.authName }}</el-tag
                    ><i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      v-for="item3 in item2.children"
                      :key="item3.id"
                      :closable="true"
                      type="warning"
                      @close="removeRightById(row, item3.id)"
                    >
                      {{ item3.authName }}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#" width="50"> </el-table-column>
        <el-table-column label="角色名称" prop="roleName"> </el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="btnEdit(row.id)"
            >
              编辑</el-button
            >
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="btnDel(row.id)"
              >删除</el-button
            >
            <el-button
              type="warning"
              size="mini"
              icon="el-icon-setting"
              @click="showSetRightDialog(row)"
              >分配权限</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分配权限 -->
    <el-dialog
      title="分配权限"
      :visible.sync="showDialog"
      width="50%"
      @close="setClose"
    >
      <el-tree
        :data="rightsList"
        show-checkbox
        node-key="id"
        ref="treeRef"
        :props="treeProps"
        default-expand-all
        :default-checked-keys="defKeys"
      >
      </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setClose">取 消</el-button>
        <el-button type="primary" @click="allotRights">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 添加角色 -->
    <el-dialog
      title="添加角色"
      :visible.sync="showAddDialog"
      width="40%"
      @close="btnCancel"
    >
      <el-form
        ref="roleForm"
        :model="formDate"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="formDate.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="formDate.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="btnCancel">取 消</el-button>
        <el-button type="primary" @click="btnOK">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getRoles,
  getRights,
  removeRightById,
  allotRights,
  addRoles,
  delRoles,
  getIdRoles,
  editSubmit,
} from "@/api/power.js";
export default {
  name: "",
  components: {},
  props: {},
  data() {
    return {
      formDate: {
        roleName: "",
        roleDesc: "",
      },
      rules: {
        roleName: [
          { required: true, message: "角色名称不能为空", trigger: "blur" },
        ],
        roleDesc: [
          { required: true, message: "角色描述不能为空", trigger: "blur" },
        ],
      },
      rolesId: "",
      roleList: [],
      showDialog: false,
      showAddDialog: false,
      rightsList: [],
      treeProps: {
        children: "children",
        label: "authName",
      },
      defKeys: [],
      roleId: "",
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getRoles();
  },
  mounted() {},
  methods: {
    async btnEdit(id) {
      this.showAddDialog = true;
      const data = await getIdRoles(id);
      this.formDate = data;
      // console.log(id);
      this.rolesId = id;
      console.log(this.formDate);
    },
    btnDel(id) {
      this.$confirm("是否删除此角色？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await delRoles(id);
          this.getRoles();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async btnOK() {
      await this.$refs.roleForm.validate();
      if (this.formDate.roleId) {
        await editSubmit({ ...this.formDate, id: this.rolesId });
      } else {
        await addRoles(this.formDate);
      }
      this.formDate = {
        roleName: "",
        roleDesc: "",
      };
      this.getRoles();
      this.showAddDialog = false;
    },
    btnCancel() {
      this.$refs.roleForm.resetFields();
      this.formDate = {
        roleName: "",
        roleDesc: "",
      };
      this.showAddDialog = false;
    },
    async getRoles() {
      this.roleList = await getRoles();
      // console.log(this.roleList)
    },
    removeRightById(role, rightId) {
      console.log(123);
      this.$confirm("确定要删除该部门吗", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const data = await removeRightById(role.id, rightId);
          console.log(data);
          role.children = data;
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async showSetRightDialog(role) {
      console.log(role);
      this.roleId = role.id;
      this.rightsList = await getRights("tree");

      // console.log(role)
      this.getLeafKeys(role, this.defKeys);
      this.showDialog = true;
    },
    getLeafKeys(node, arr) {
      if (!node.children) {
        return arr.push(node.id);
      }
      node.children.forEach((item) => this.getLeafKeys(item, arr));
    },

    setClose() {
      this.defKeys = [];
      this.showDialog = false;
    },
    async allotRights() {
      const keys = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys(),
      ];
      // 将数组转换为 , 拼接的字符串
      const idStr = keys.join(",");
      console.log(idStr);
      console.log(keys);
      const data = await allotRights(this.roleId, { rids: idStr });
      console.log(data);
      this.$message.success("分配权限成功");
      this.getRoles();
      this.showDialog = false;
    },
  },
};
</script>

<style scoped lang="less">
.el-tag {
  margin: 7px;
}
/deep/.bgtop {
  border-top: 1px solid #eee;
}
.bgbottom {
  border-bottom: 1px solid #eee;
}
/deep/ .el-table__expanded-cell[class*="cell"] {
  padding: 20px 50px;
}
</style>
